package com.kgc.dao;

import com.kgc.entity.Payment;
import com.kgc.utils.PageUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class paymentDao extends BaseDao {

    public int count() {
        String sql = "select count(*) from payment";
        try {
            ResultSet rs = super.getExecuteQuery(sql);
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }

    public List<Payment> selectAll() {
        List<Payment> list = new ArrayList<>();
        String sql = "select * from payment";
        try {
            ResultSet rs = super.getExecuteQuery(sql);
            while (rs.next()) {
                list.add(new Payment(rs.getInt(1), rs.getString(2)));
            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return null;
    }


    public List<Payment> selectByPage(PageUtils pageUtils, String cy1name) {
        List<Payment> list = new ArrayList<>();
        String sql = "select * from payment ";
        if (cy1name != null && !"".equals(cy1name)) {
            sql += "where name like '%" + cy1name + "%'";
        }
        sql += " limit ?,?";
        try {
            ResultSet rs = super.getExecuteQuery(sql,pageUtils.start(),pageUtils.getPageSize());
            while (rs.next()) {
                list.add(new Payment(rs.getInt(1), rs.getString(2)));
            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return null;
    }

    public int add( String type) {
        String sql = "INSERT INTO `wyadmin01`.`payment`(`id`, `name`) VALUES (null, ?)";
        return super.getExecuteUpdate(sql, type);

    }

    public int deleteById(Integer id) {
        String sql = "DELETE from `payment` WHERE id=?";
        return super.getExecuteUpdate(sql,id);
    }

    public int updateByID(Integer id, String name) {
        String sql = " UPDATE `wyadmin01`.`payment` SET `name` = ? WHERE `id` = ?;";
        return super.getExecuteUpdate(sql,name,id);
    }

}
